import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 * @author zlss
 * @date 2025/5/30
 * @description
 */
public class Cell extends StackPane {
    private int x;
    private int y;
    private String color;
    private StackPane cellPane;
    private Rectangle background;
    private Text text;
    private boolean matched;

    public Cell(int x, int y, String color) {
        this.x = x;
        this.y = y;
        this.color = color;

        // 创建单元格UI
        cellPane = new StackPane();
        background = new Rectangle(60, 60);
        // 修改：添加对 color 为 null 的处理
        background.setFill(color != null ? Paint.valueOf(color) : Color.TRANSPARENT);
        background.setStroke(Color.BLACK);
        background.setStrokeWidth(1);

        text = new Text();
        text.setFont(Font.font(24));

        cellPane.getChildren().addAll(background, text);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getColorName() {
        return color;
    }

    public StackPane getCellPane() {
        return cellPane;
    }

    public void highlight() {
        background.setStroke(Color.WHITE);
        background.setStrokeWidth(3);
    }

    public void unhighlight() {
        background.setStroke(Color.BLACK);
        background.setStrokeWidth(1);
    }

    public void updateAppearance() {
        // 修改：添加对 color 为 null 的处理
        background.setFill(color != null ? Paint.valueOf(color) : Color.TRANSPARENT);
    }

    public void setMatched(boolean matched) {
        this.matched = matched;
        if (matched) {
            background.setStroke(Color.WHITE);
            background.setStrokeWidth(3);
        } else {
            background.setStroke(Color.BLACK);
            background.setStrokeWidth(1);
        }
    }

    public boolean isMatched() {
        return matched;
    }
}